<?php
/**
 * @file : articlevote.php
 * @date : 2015-02-03 15:57:42
 * @author : yuebin
 */
namespace haibao\cms\business;
use haibao\cms\data\ArticleVote as ArticleVoteData;
use haibao\cms\model\filter\ArticleVoteByUser as ArticleVoteByUserFilter;
use haibao\frame\data\query\Condition;
class ArticleVote extends Base{
	
	/**
	 * 明星库首页 判断当前用户是否已投当前明星猜猜猜
	 */
	public static function checkUserVote($userId,$guessId){
		$articleVoteData = new ArticleVoteData;
		return $articleVoteData->checkUserVote($userId,$guessId);
	}

	
	/**
	 * 添加文章投票
	 * @param : object \haibao\cms\model\data\ArticleVote
	 */
	
	public static function addArticleVote($articleVoteModel){
		
		if(!$articleVoteModel->ArticleId){
			throw new BusinessException('文章ID为空',self::DATA_EMPTY_NAME);
		}
		if(!$articleVoteModel->ArticleVoteProperty){
			throw new BusinessException('文章投票内容为空',self::DATA_EMPTY_NAME);
		}
		$articleVoteData = new ArticleVoteData;
		$articleVoteData->addArticleVote($articleVoteModel);
		
		if($articleVoteModel->ImgIds){
			$articleVoteModel->ImgIds = array_values($articleVoteModel->ImgIds);
			$imageLibraryData = new \haibao\cms\data\ImageLibrary();
			$imageLibraryData->updateImageStatus($articleVoteModel->ImgIds,\haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,self::getCurrentUser()->UserName);
		}
		self::saveOperateHistory(self::DATA_ERROR_ARTICLE_VOTE_ADD, $articleVoteModel->toArray(false));
	}
	
	/**
	 * 编辑文章投票
	 * @param : object \haibao\cms\model\data\ArticleVote
	 */
	public static function editArticleVote($articleVoteModel){
		if(!$articleVoteModel->Id){
			throw new BusinessException('文章投票ID为空',self::DATA_EMPTY_NAME);
		}
		if(!$articleVoteModel->ArticleVoteProperty && !$articleVoteModel->ArticleVotePropertyNew){
			throw new BusinessException('文章投票内容为空',self::DATA_EMPTY_NAME);
		}
		
		$articleVoteData = new ArticleVoteData;
		$articleVoteData->editArticleVote($articleVoteModel);
		if($articleVoteModel->ImgIds){
			$articleVoteModel->ImgIds = array_values($articleVoteModel->ImgIds);
			$imageLibraryData = new \haibao\cms\data\ImageLibrary();
			$imageLibraryData->updateImageStatus($articleVoteModel->ImgIds,\haibao\cms\model\data\ImageLibrary::STATUS_ENABLE,self::getCurrentUser()->UserName);
		}

		self::saveOperateHistory(self::DATA_ERROR_ARTICLE_VOTE_UPDATE, $articleVoteModel->toArray(false));
		
	}
	/**
	 * 根据filter获取文章投票信息
	 * @param : Object \haibao\cms\model\filter\ArticleVote
	 */
	public static function getOneVoteByFilter($filter){
		
		$data = new ArticleVoteData;
		//print_r($data->getOne($filter));
		return $data->getOne($filter);
	}
	
	public static function getAllVoteByFilter($filter){
		$data = new ArticleVoteData;
		return $data->getAll($filter);
	}
	
	/**
	 * 判断当前用户是否已经投票
	 * @param : int $articleVoteId 文章投票ID
	 * @param : int $votePropertyId 当前投票属性ID
	 */
	public static function checkCurrentArticleIsVote($articleVoteId, $votePropertyId = null){
		
		if(!$articleVoteId){
			throw new BusinessException('文章投票ID为空',self::DATA_EMPTY_NAME);
		}
		
		$filter = new ArticleVoteByUserFilter;
		$filter->select(
			array(
				ArticleVoteByUserFilter::CONDITION_FIELD_NAME_CREATE_BY
			)
		);
		
		$filter->where(ArticleVoteByUserFilter::CONDITION_FIELD_NAME_ARTICLE_VOTE_ID,Condition::CONDITION_EQUAL,$articleVoteId);
		if($votePropertyId){
			$filter->where(ArticleVoteByUserFilter::CONDITION_FIELD_NAME_ARTICLE_VOTE_PROPERTY_ID, Condition::CONDITION_EQUAL, $votePropertyId);
		}
		
		$data = new ArticleVoteData;
		return $data->getAll($filter);
		
	}
	/**
	 * 根据文章投票ID删除投票信息
	 * @param : int $articleVoteId
	 * @param : int $articleId
	 */
	public static function deleteArticleVoteByVoteId($articleVoteId,$articleId){
		if(!$articleVoteId){
			throw new BusinessException('文章投票ID为空',self::DATA_EMPTY_NAME);
		}
		$data = new ArticleVoteData;
 	 	self::saveOperateHistory(self::DATA_ERROR_ARTICLE_VOTE_DEL, array($articleId,$articleVoteId));
		return $data->deleteArticleVoteByVoteId($articleVoteId,$articleId);
	}
	
	/**
	 * 投票
	 */
	public static function voteArticleImage($articleId, $voteId, $pid,$userId =0){
	    if(!$articleId || !$voteId){
	        throw new BusinessException('投票信息错误',self::DATA_EMPTY_NAME);
	    }
	    $data = new ArticleVoteData();
	    
	    if($userId){
	    	\haibao\user\common\ScoreRule::operateScore($userId,\haibao\user\common\ScoreRule::TYPE_VOTE);
	    }
	     
	    return $data->voteArticleImage($articleId, $voteId, $pid);
	}
}